package com.fivebrms.hibernate.entity.dao;

import java.util.Collection;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.fivebrms.hibernate.entity.model.LoginForm;

public class LoginDaoHibernate implements LoginDAO
{
	final Logger	       log	= LoggerFactory.getLogger(LoginDaoHibernate.class);

	private SessionFactory	sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory()
	{
		return this.sessionFactory;
	}

	public Collection<LoginForm> findAll()
	{
		Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
		try
		{
			log.info("start login find all..");

			Collection<LoginForm> products = session.createQuery("from LoginForm").list();

			log.info("end login find all..");

			return products;
		}
		catch (HibernateException e)
		{
			throw SessionFactoryUtils.convertHibernateAccessException(e);
		}
	}

	public void createLoginForm(LoginForm loginCredentials) throws DataAccessException
	{
		log.info("..1..login.." + loginCredentials);
		log.info("..2..login.." + this.getSessionFactory());
		Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
		log.info("..3..login..->" + session);
		session.save(loginCredentials);
		log.info("..4..login.." + loginCredentials);
	}

	public void updateLoginForm(LoginForm loginForm) throws DataAccessException
	{
		Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
		session.update(loginForm);
	}

	public void deleteLoginForm(LoginForm loginForm) throws DataAccessException
	{
		Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
		session.delete(loginForm);
	}

}
